﻿#region Copyright(C) 2013-2013 NCS Pte. Ltd. All rights reserved.
// ==================================================================================================
// Copyright(C) 2010 NCS Pte. Ltd. All rights reserved.
//
// SYSTEM NAME      : iConnect .Net Application Framework
// COMPONENT ID     : NCS.IConnect.Core.EntLib
// COMPONENT DESC   : Microsoft Enterprise Library Adapter.
//
// CREATED DATE/BY  : 18 Dec 2013 / Jiang Jin Nan
//
// REVISION HISTORY :
// DATE/BY            SR#/CS/PM#/OTHERS       DESCRIPTION OF CHANGE
// ==================================================================================================
#endregion

using Microsoft.Practices.EnterpriseLibrary.Data;
using NCS.IConnect.Core.Data;
using NCS.IConnect.Core.IoC;
using NCS.IConnect.Core.Utilities;
using System.Data.Common;
using System.Reflection;

namespace NCS.IConnect.Core.EntLib.Data
{
    [MapTo(typeof(IParameterCache), Lifetime = Lifetime.Singleton)]
    internal class ParameterCacheWrapper: IParameterCache
    {
        private static ParameterCache cache;
        public static ParameterCache ParameterCache
        {
            get
            {
                return cache ?? (cache = (ParameterCache)(typeof(Microsoft.Practices.EnterpriseLibrary.Data.Database)
                    .GetField("parameterCache", BindingFlags.Static | BindingFlags.NonPublic).GetValue(null)));
            }
        }

        public void SetParameters(DbCommand command, NCS.IConnect.Core.Data.Database database)
        {
            Guard.ArgumentNotNull(command, "command");
            Guard.InstanceIsAssignable(typeof(DatabaseWrapper), database, "database");
            ParameterCache.SetParameters(command, ((DatabaseWrapper)database).Database);
        }
    }
}
